#include "file.h"

#define DEBUG_ON 0

CONFIG_FILE config_info;

extern "C" void save_file(void)
{ 
  File f = SPIFFS.open(config_file_name,"a+");

  f.print(config_info.wifi_name);f.print('\n');
  f.print(config_info.wifi_pass);f.print('\n');
  f.print(config_info.bps);f.print('\n');
  f.print(config_info.server_ip);f.print('\n');
  f.print(config_info.server_port);f.print('\n');
  f.print(config_info.user_name);f.print('\n');
  f.print(config_info.user_psss);f.print('\n');
  f.print(config_info.device_id);f.print('\n');
  f.print(config_info.sub_topic);f.print('\n');
  f.print(config_info.pub_topic);f.print('\n');
  
  f.close();  
}
extern "C" void read_file(void)
{
  File f = SPIFFS.open(config_file_name,"r");

  String temp;
  
  temp  = f.readStringUntil('\n');
  memcpy(config_info.wifi_name,temp.c_str(),temp.length());
  
  temp  = f.readStringUntil('\n');
  memcpy(config_info.wifi_pass,temp.c_str(),temp.length());
  
  temp  = f.readStringUntil('\n');
  memcpy(config_info.bps,temp.c_str(),temp.length());

  temp  = f.readStringUntil('\n');
  memcpy(config_info.server_ip,temp.c_str(),temp.length());

  temp  = f.readStringUntil('\n');
  memcpy(config_info.server_port,temp.c_str(),temp.length());

  temp  = f.readStringUntil('\n');
  memcpy(config_info.user_name,temp.c_str(),temp.length());

  temp  = f.readStringUntil('\n');
  memcpy(config_info.user_psss,temp.c_str(),temp.length());

  temp  = f.readStringUntil('\n');
  memcpy(config_info.device_id,temp.c_str(),temp.length());

  temp  = f.readStringUntil('\n');
  memcpy(config_info.sub_topic,temp.c_str(),temp.length());

  temp  = f.readStringUntil('\n');
  memcpy(config_info.pub_topic,temp.c_str(),temp.length());


  
  f.close();

  #if DEBUG_ON
  Serial.println("*************************\nvoid read_info(void)");
  Serial.println("save file :\n*************************"); 
  Serial.println(config_info.wifi_name );
  Serial.println(config_info.wifi_pass );
  Serial.println(config_info.bps);
  Serial.println(config_info.server_ip );
  Serial.println(config_info.server_port );
  Serial.println(config_info.user_psss );
  Serial.println(config_info.user_name );
  Serial.println(config_info.device_id );
  Serial.println(config_info.sub_topic );
  Serial.println(config_info.pub_topic );
  #endif  
}
void print_file()
{
  Serial.println("====================================");
  Serial.println("           save file                ");
  Serial.println("====================================");
  Serial.println(config_info.wifi_name );
  Serial.println(config_info.wifi_pass );
  Serial.println(config_info.bps);
  Serial.println(config_info.server_ip );
  Serial.println(config_info.server_port );
  Serial.println(config_info.user_psss );
  Serial.println(config_info.user_name );
  Serial.println(config_info.device_id );
  Serial.println(config_info.sub_topic );
  Serial.println(config_info.pub_topic );
  Serial.println("====================================");  
}
